import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUi from 'element-ui'
import ViewUI from 'view-design'
import moment from 'moment'
import 'view-design/dist/styles/iview.css'
import Vab from '@/utils/vab'
import { checkAuth } from '@/api/setup'
import { getSuperPower } from '@/api/plug.js'
import Api from './api/index.js'
var jurisdiction = null

Vue.prototype.$api = Api

Vue.config.productionTip = false
Vue.use(ElementUi)
Vue.use(ViewUI)
Vue.use(Vab)

Vue.prototype.$moment = moment
Vue.prototype.bus = new Vue()

getSuperPower().then(res => {
  jurisdiction = res.data
})

/** 验证用户是否授权 **/
router.beforeEach(async (to, from, next) => {
  const routerJusri = ['cashReg', 'carRegT', 'card', 'vipConfig', 'vipList']
  if (to.path !== '/login') {
    const { type, data } = await checkAuth()
    if (type == '2') {
      if (to.path !== '/activationCode') {
        return next({ path: '/activationCode' })
      } else {
        next()
      }
    }
    if (type == '3') {
      if (data) {
        window.open(data)
      }
    }
    if (type == '4') {
      if (data) {
        return next({ path: data })
      }
    } else {
      if (to.path === '/activationCode') {
        return next({ path: '/' })
      }
      if (routerJusri.indexOf(to.name) >= 0) {
        if (jurisdiction.plug_ids.indexOf('cashier') >= 0) {
          next()
        } else if (jurisdiction.plug_ids.indexOf('payVip') >= 0) {
          next()
        } else {
          next('/404')
        }
      } else {
        next()
      }
    }
  } else {
    next()
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
